/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name:'news_list', params:{currentPage:$route.params.currentPage}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>新闻管理</h3>
            <h5>网站系统新闻新增与编辑</h5>
          </div>
        </div>
      </div>
      <el-form ref="news" :model="news" label-width="200px">
        <el-form-item label="标题" required>
          <el-input v-model="news.title" size="mini"></el-input>
          <p class="err-msg" v-if="errResult.title && !news.title">{{errResult.title}}</p>
        </el-form-item>
        <el-form-item label="所属分类" required>
          <el-select v-model="news.cat_id" size="small">
            <el-option
              v-for="item in newsCats"
              :key="item.cat_id"
              :label="item.cat_name"
              :value="item.cat_id"
            ></el-option>
          </el-select>
          <p class="err-msg" v-if="errResult.cat_id && !news.cat_id">{{errResult.cat_id}}</p>
          <p class="notic">当选择发布"商城公告"时，还需要设置下面的"出现位置"项</p>
        </el-form-item>
        <el-form-item label="新闻标签" required>
          <el-checkbox-group v-model="newsTags">
            <el-checkbox v-for="(key, tags) in tagsMap" :label="tags" :key="key">{{key}}</el-checkbox>
          </el-checkbox-group>
          <p class="err-msg" v-if="errResult.tags && newsTags.length == 0">{{errResult.tags}}</p>
        </el-form-item>
        <el-form-item label="seo关键字">
          <el-input v-model="news.keywords" size="mini">
            <template slot="append">Http://</template>
          </el-input>
          <p class="notic">用于seo 搜索引擎友好</p>
        </el-form-item>
        <el-form-item label="链接">
          <el-input v-model="news.link" size="mini"></el-input>
          <p class="notic">当填写"链接"后点击文章标题将直接跳转至链接地址，不显示文章内容。链接格式请以http://开头</p>
        </el-form-item>
        <el-form-item label="发布时间">
          <div class="block">
            <el-date-picker
              size="mini"
              v-model="publishTime"
              type="datetime"
              value-format="timestamp"
              placeholder="选择日期时间"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="网页描述">
          <el-input type="textarea" v-model="news.description" style="width:350px" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="新闻导图一" v-show="news.type != 0">
          <el-upload
            class="avatar-uploader"
            :action="apiHead + '/mall/upload?type=news'"
            :show-file-list="false"
            :on-success="handleThumb"
            :before-upload="beforeAvatarUpload"
          >
            <el-image v-if="news.thumb" :src="news.thumb" style="width: 100px; height: 100px"></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="notic">请上传图片格式文件</p>
        </el-form-item>
        <el-form-item label="新闻导图二" v-show="news.type  > 2">
          <el-upload
            class="avatar-uploader"
            :action="apiHead + '/mall/upload?type=brand'"
            :show-file-list="false"
            :on-success="handleThumb2"
            :before-upload="beforeAvatarUpload"
          >
            <el-image v-if="news.thumb2" :src="news.thumb2" style="width: 100px; height: 100px"></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="notic">请上传图片格式文件</p>
        </el-form-item>
        <el-form-item label="新闻导图三" v-show="news.type  > 2">
          <el-upload
            class="avatar-uploader"
            :action="apiHead + '/mall/upload?type=brand'"
            :show-file-list="false"
            :on-success="handleThumb3"
            :before-upload="beforeAvatarUpload"
          >
            <el-image v-if="news.thumb3" :src="news.thumb3" style="width: 100px; height: 100px"></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="notic">请上传图片格式文件</p>
        </el-form-item>
        <el-form-item label="导图展现样式">
          <el-radio-group v-model="news.type">
            <el-radio :label="0">不显示图片</el-radio>
            <el-radio :label="1">左边图片</el-radio>
            <el-radio :label="2">右边图片</el-radio>
            <el-radio :label="3">上边多图片</el-radio>
            <el-radio :label="4">下边多图片</el-radio>
          </el-radio-group>
          <p class="notic" style="color: red">注：左右显示图片只显示导图一，上下多图片显示为3张比例</p>
        </el-form-item>
        <el-form-item label="显示">
          <el-switch v-model="news.is_open" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="审核：" v-if="isCheck">
          <el-switch
            v-model="news.check_type"
            active-value="1"
            inactive-value="2"
            active-text="审核通过"
            inactive-text="拒绝通过"
          ></el-switch>
        </el-form-item>
        <el-form-item label="文章内容" required>
          <Ueditor :value="ueditor.value" :config="ueditor.config" ref="ue" v-if="ueditor.isShow"></Ueditor>
          <p
            class="err-msg"
            v-if="errResult.content && $refs.ue.getUEContent() == ''"
          >{{errResult.content}}</p>
        </el-form-item>
        <el-button
          type="primary"
          @click="submit()"
          :loading="isLoading"
          style="margin-left:200px"
        >确认提交</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getNews, getNewsCats, updateNews, addNews } from "@/utils/api";
import Ueditor from "@/components/ueditor/index";
export default {
  data() {
    return {
      article_id: this.$route.params.id,
      news: { type: 0 },
      publishTime: new Date().getTime() + 86400 * 1000,
      newsTags: [],
      newsCats: [],
      tagsMap: { 0: "最新", 1: "热门", 2: "推荐", 3: "精品" },
      ueditor: {
        isShow: false,
        value: "",
        config: {
          serverUrl: this.apiHead + "/mall/ueditor/config?path=news",
          zIndex: 999,
          initialFrameWidth: "80%", //初化宽度
          initialFrameHeight: 300, //初化高度
          focus: false, //初始化时，是否让编辑器获得焦点true或false
          maximumWords: 99999,
          removeFormatAttributes:
            "class,style,lang,width,height,align,hspace,valign", //允许的最大字符数 'fullscreen',
          pasteplain: false, //是否默认为纯文本粘贴。false为不使用纯文本粘贴，true为使用纯文本粘贴
          autoHeightEnabled: true,
        },
      },
      isCheck: this.$route.params.isCheck,
      isLoading: false,
      errResult: {},
    };
  },
  created() {
    this.getNewsCats();
    if (this.$route.params.id) {
      this.getNews();
    } else {
      this.ueditor.isShow = true;
    }
  },
  components: {
    Ueditor,
  },
  methods: {
    getNews() {
      var that = this;
      getNews(this.article_id).then(function (res) {
        that.publishTime = res.publish_time * 1000;
        that.newsTags = res.tags.split(",");
        res.check_type = String(res.check_type);
        that.ueditor.value = res.content;
        res.is_open = String(res.is_open);
        that.news = res;
        that.ueditor.isShow = true;
      });
    },
    getNewsCats() {
      var that = this;
      getNewsCats().then(function (res) {
        that.newsCats = res;
      });
    },
    submit() {
      var that = this;
      this.isLoading = true;
      this.news.publish_time = this.publishTime / 1000;
      this.news.tags = this.newsTags.join(",");
      this.news.content = this.$refs.ue.getUEContent();
      if (this.news.article_id) {
        //编辑
        updateNews(that.news).then(function (res) {
          if (res.status == 1) {
            that.$message.success({
              message: "保存成功",
              onClose: function () {
                that.$router.push({ name: "news_list" });
              },
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        this.news.source = 0; //添加是指定来源为0 表示管理员
        addNews(that.news).then(function (res) {
          if (res.status == 1) {
            that.$message.success({
              message: "添加成功",
              onClose: function () {
                that.$router.push({ name: "news_list" });
              },
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    },
    handleThumb(res) {
      this.news.thumb = res.result;
      this.$forceUpdate();
    },
    handleThumb2(res) {
      this.news.thumb2 = res.result;
      this.$forceUpdate();
    },
    handleThumb3(res) {
      this.news.thumb3 = res.result;
      this.$forceUpdate();
    },
    beforeAvatarUpload(file) {
      const isPic =
        file.type === "image/gif" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPic) {
        this.$message.error("上传图片只能是 JPG、JPEG、PNG、GIF 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isPic && isLt2M;
    },
  },
};
</script>

<style scoped>
>>> .el-input__inner {
  width: 300px;
}
.avatar-uploader-icon {
  line-height: 100px;
}
</style>
